<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :label="item.id" :key="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 10px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from "@/api/setting";
import { getUserDetailById } from "@/api/user";
import { assignRoles } from "@/api/employees";
export default {
  props: {
    curId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      roleIds: [],
      list: [],
    };
  },
  created() {
    // 加载所有的角色
    this.loadRoles();
    // 加载当前用户拥有的角色属性
    this.loadRolesById();
  },
  methods: {
    
    async hSave() {
      // 调用接口
      await assignRoles({ id: this.curId, roleIds: this.roleIds });
      this.$message.success("分配角色成功");
      // 通知父组件关闭弹层
      this.$emit("success");
    },
    async loadRolesById() {
      // 根据用户的id获取他的角色
      const res = await getUserDetailById(this.curId);
      console.log("res", res);
      // 数据回填
      this.roleIds = res.data.roleIds;
    },
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 10 });
      console.log(res);
      this.list = res.data.rows;
    },
    closeDialog() {
      this.$emit("close");
    },
  },
};
</script>